<?php

namespace common\models\searchs;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\DhArticle;
use yii\db\Query;
/**
 * SearchDhArticle represents the model behind the search form about `common\models\DhArticle`.
 */
class SearchDhArticle extends DhArticle
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['ID'], 'integer'],
            [['TitleIntact', 'Subheading', 'Author', 'CopyFrom', 'Keyword', 'Intro', 'PaginationType', 'Content', 'Stars'], 'safe'],
            [['MaxCharPerPage'], 'number'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $condition = '';
        $nodes = \common\models\DhNodes::findOne(["NodeID"=>$params['nodeid']]);
       
        if($params['nodeid'] == 10 || $nodes->ParentID == 10){
             
            $data  = (new Query())->select('*')->from("dh_nodes")->Where(['ParentID'=>10])->all();
            
            $category = '';
            
            foreach ($data as $value) {
                $category .= $value['NodeID'] . ',';
            }
            $category = substr($category, 0, -1);
            $condition = $category.',10';
            $condition = 'NodeID in(' . $condition . ')';
            
        }else{
            
            $condition = $params['nodeid'];
            $condition = 'NodeID in(' . $condition . ')';
        }
        
//        $query = (new Query())->select("a.*,b.*")->from("dh_article as a")->leftJoin("dh_commonmodel as b")->where($condition)->
//                orderBy("b.stick DESC,b.Priority ASC,b.UpdateTime DESC");
        $query = DhArticle::find()->where($condition)->orderBy('ID DESC');
//        print_r($query);die();
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        if (!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }

        $query->andFilterWhere([
            'ID' => $this->ID,
            'NodeID'=>$this->NodeID,
            'MaxCharPerPage' => $this->MaxCharPerPage,
        ]);

        $query->andFilterWhere(['like', 'TitleIntact', $this->TitleIntact])
            ->andFilterWhere(['like', 'Subheading', $this->Subheading])
            ->andFilterWhere(['like', 'Author', $this->Author])
            ->andFilterWhere(['like', 'CopyFrom', $this->CopyFrom])
            ->andFilterWhere(['like', 'Keyword', $this->Keyword])
            ->andFilterWhere(['like', 'Intro', $this->Intro])
            ->andFilterWhere(['like', 'PaginationType', $this->PaginationType])
            ->andFilterWhere(['like', 'Content', $this->Content])
            ->andFilterWhere(['like', 'Stars', $this->Stars]);

        return $dataProvider;
    }
}
